package com.cs.framework.ui.dao;

import java.util.List;

import javax.sql.DataSource;

import com.cs.framework.jdbc.common.dao.JdbcTemplateDao;
import com.cs.framework.ui.dto.CSFieldDto;
import com.cs.framework.ui.dto.CSFieldProSetDto;
import com.cs.framework.ui.dto.CSPageDto;
import com.cs.framework.ui.dto.CSPanelButtonDto;
import com.cs.framework.ui.dto.CSPanelDto;
import com.cs.framework.ui.dto.CSPanelLayoutDto;
import com.cs.framework.ui.dto.CSPanelProSetDto;

/**
 * 处理框架UI定义的dao
 * 
 * @author hedongzhou
 * 
 */
public class UIDefDao extends JdbcTemplateDao {

	public UIDefDao(DataSource dataSource) {
		super(dataSource);
	}

	/**
	 * 查询页面
	 * 
	 * @param pageId
	 *            页面ID
	 * @return
	 */
	public <T extends CSPageDto> T getPage(String pageId, Class<T> cls) {
		return queryOne("select * from cs_page where page_id = ?", cls, pageId);
	}

	/**
	 * 查询面板
	 * 
	 * @param <T>
	 * @param panelId
	 *            面板ID
	 * @param cls
	 *            类型
	 * @return
	 */
	public <T extends CSPanelDto> T getPanel(String panelId, Class<T> cls) {
		return queryOne("select * from cs_panel where panel_id = ?", cls, panelId);
	}

	/**
	 * 查询面板按钮
	 * 
	 * @param panelId
	 *            面板ID
	 * @return
	 */
	public List<CSPanelButtonDto> getPanelButtonList(String panelId) {
		return queryList("select * from cs_panel_button where panel_id = ? order by button_no", CSPanelButtonDto.class,
				panelId);
	}

	/**
	 * 查询面板布局
	 * 
	 * @param panelId
	 *            面板ID
	 * @return
	 */
	public List<CSPanelLayoutDto> getPanelLayoutList(String panelId) {
		return queryList("select * from cs_panel_layout a where panel_id = ? order by child_no",
				CSPanelLayoutDto.class, panelId);
	}

	/**
	 * 查询面板属性
	 * 
	 * @param panelId
	 *            面板ID
	 * @return
	 */
	public List<CSPanelProSetDto> getPanelProSetList(String panelId) {
		return queryList("select * from cs_panel_pro_set where panel_id = ? and parent_id is null",
				CSPanelProSetDto.class, panelId);
	}

	/**
	 * 查询面板属性
	 * 
	 * @param panelId
	 *            面板ID
	 * @param parentId
	 *            父面板ID
	 * @return
	 */
	public List<CSPanelProSetDto> getPanelProSetList(String panelId, String parentId) {
		return queryList(
				"select * from (select * from cs_panel_pro_set where panel_id = ? and parent_id is null union all select * from cs_panel_pro_set where panel_id = ? and parent_id = ?) t order by parent_id DESC",
				CSPanelProSetDto.class, panelId, panelId, parentId);
	}

	/**
	 * 查询字段
	 * 
	 * @param <T>
	 * @param fieldId
	 *            字段ID
	 * @param cls
	 *            类型
	 * @return
	 */
	public <T extends CSFieldDto> T getField(String fieldId, Class<T> cls) {
		return queryOne("select * from cs_field where field_id = ?", cls, fieldId);
	}

	/**
	 * 查询字段属性
	 * 
	 * @param fieldId
	 *            字段ID
	 * @return
	 */
	public List<CSFieldProSetDto> getFieldProSetList(String fieldId) {
		return queryList("select * from cs_field_pro_set where field_id = ? and parent_id is null",
				CSFieldProSetDto.class, fieldId);
	}

	/**
	 * 查询字段属性
	 * 
	 * @param fieldId
	 *            字段ID
	 * @param parentId
	 *            父面板ID
	 * @return
	 */
	public List<CSFieldProSetDto> getFieldProSetList(String fieldId, String parentId) {
		return queryList(
				"select * from (select * from cs_field_pro_set where field_id = ? and parent_id is null union all select * from cs_field_pro_set where field_id = ? and parent_id = ?) t order by parent_id DESC",
				CSFieldProSetDto.class, fieldId, fieldId, parentId);
	}

}
